package me.zk.practice1.hw;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author zhangkai
 * @date 2023/8/11 12:08
 */
public class HWBJJ01 {

    private static int[][] direction = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};

    //长
    private static int row;
    //宽
    private static int h;

    //数组

    private static String[][] strs;

    //因为有重叠部分，所以要用一个copy
    private static String[][] copyStr;
    //
    private static List<int[]> gzList = new ArrayList<>();

    //YES NO NO NO
    //NO NO NO NO
    //NO NO NO NO
    //NO NO NO NO
    //1
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> list = new ArrayList<>();
        while (scanner.hasNext()) {
            String s = scanner.nextLine();
            if (s.equals("1")) {
                break;
            }
            list.add(s);
        }
        row = list.size();
        h = list.get(0).split(" ").length;

        strs = new String[row][h];
        copyStr =  new String[row][h];
        int noTotal = 0;
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            String[] s2 = s.split(" ");
            for (int j = 0; j < s2.length; j++) {
                strs[i][j] = s2[j];
                copyStr[i][j] = s2[j];
                if ("NO".equals(s2[j])) {
                    noTotal++;
                }
            }
        }
        int day = 0;
        boolean isCon= true;
        while (noTotal != 0 && isCon) {
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < h; j++) {
                    if ("YES".equals(strs[i][j])) {
                        get(i,j);
                    }
                }
            }
            if (gzList.size()>0) {
                day++;
                for (int[] gz : gzList) {
                    strs[gz[0]][gz[1]] = "YES";
                }
                noTotal-= gzList.size();
                gzList.clear();

            } else {
                isCon= false;
            }


        }
        if (noTotal == 0) {
            System.out.println(day);
        } else {
            System.out.println(-1);
        }

    }
    public static void get(int i, int j) {
        for (int[] d : direction) {
            int ni = i+d[0];
            int nj = j+d[1];
            if (ni >=0 && nj>=0 && ni<row && nj < h && copyStr[ni][nj].equals("NO")) {
                copyStr[ni][nj] = "YES";
                gzList.add(new int[]{ni,nj});
            }

        }

    }

    public static void gaizao(int x, int y) {

        //上
        if (x > 0 && copyStr[x - 1][y].equals("NO")) {
            copyStr[x - 1][y] = "YES";
            gzList.add(new int[]{x - 1, y});
        }
        //下
        if (x < row - 1 && copyStr[x + 1][y].equals("NO")) {
            copyStr[x + 1][y] = "YES";
            gzList.add(new int[]{x + 1, y});
        }
        //左
        if (y > 0 && copyStr[x][y - 1].equals("NO")) {
            copyStr[x][y - 1] = "YES";
            gzList.add(new int[]{x, y - 1});
        }
        //右
        if (y < h - 1 && copyStr[x][y + 1].equals("NO")) {
            copyStr[x][y + 1] = "YES";
            gzList.add(new int[]{x, y + 1});
        }
    }


}
